package impl

import (
	"context"
	"github.com/sirupsen/logrus"
	"monoblog/common"
	"monoblog/common/register"
	"monoblog/domain/dao"
	"monoblog/domain/dto"
	"monoblog/domain/model"
	"monoblog/domain/service"
	"time"
)

func init() {
	register.RegisterInstance[service.FeedbackService]("FeedbackService", func() service.FeedbackService {
		return &FeedbackServiceImpl{
			feedbackRepository: dao.NewFeedbackRepository(),
		} // 返回唯一实例
	})
}

type FeedbackServiceImpl struct {
	feedbackRepository *dao.FeedbackRepository
}

func (impl *FeedbackServiceImpl) Add(ctx context.Context, form dto.FeedbackReqForm) error {
	feedback := &model.Feedback{
		Kind:       model.FeedbackKind(form.Kind),
		Content:    form.Content,
		Status:     0,
		Contact:    form.Contact,
		CreateTime: time.Now(),
	}
	if form.LoginUser != nil {
		feedback.CreateBy = form.LoginUser.UID
	}
	err := impl.feedbackRepository.Add(feedback)
	if err != nil {
		logrus.Errorf("[aid: %s] SubmitFeedback failed, err: %v", ctx.Value("aid"), err)
		return err
	}
	return nil
}

func (impl *FeedbackServiceImpl) FindListByUserId(ctx context.Context, userId string) ([]*model.Feedback, error) {
	feedbacks, _, err := impl.feedbackRepository.FindListByUserId(userId, common.BaseQuery{Page: 1, Size: 100})
	if err != nil {
		logrus.Errorf("[aid: %s] FindListByUserId failed, userId: %s, err: %v", ctx.Value("aid"), userId, err)
		return nil, err
	}

	return feedbacks, nil
}

func (impl *FeedbackServiceImpl) FindList(ctx context.Context) ([]*model.Feedback, error) {
	feedbacks, _, err := impl.feedbackRepository.FindList(common.BaseQuery{Page: 1, Size: 100})
	if err != nil {
		logrus.Errorf("[aid: %s] FindList failed, err: %v", ctx.Value("aid"), err)
		return nil, err
	}

	return feedbacks, nil
}
